package cn.kgc.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: mosin
 * @version: v1.0  2022/4/19
 */
public final class SqlSessionUtil {

    private SqlSessionUtil(){ }

    public static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        try {
            InputStream   rs = Resources.getResourceAsStream("config/mybatis-config.xml");
            // SqlSessionFactory 创建工厂对象
            SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = sfb.build(rs);
            //SqlSession  sql会话对象
            sqlSession = factory.openSession(true);  // 建立连接
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  sqlSession;
    }


}
